//
//  LogInViewController.m
//  Fengly_Nuo_Movie
//
//  Created by dllo on 16/1/1.
//  Copyright © 2016年 Fengly_Nuo. All rights reserved.
//

#import "LogInViewController.h"
#import "LTView.h"
#import "RegisterViewController.h"

// MARK:VIEW_CENTER_X:屏中心x轴
#define VIEW_CENTER_X self.view.center.x

// MARK:VIEW_CENTER_Y:屏中心y轴
#define VIEW_CENTER_Y self.view.center.y

// MARK:HEIGHT:屏高
#define HEIGHT self.view.frame.size.height

// MARK:WIDTH:屏宽
#define WIDTH self.view.frame.size.width

@interface LogInViewController ()<UITextFieldDelegate>

@property (nonatomic, retain) LTView *userLTView;
@property (nonatomic, retain) LTView *pwdLTView;
@property (nonatomic, retain) UILabel *userPromptLabel;
@property (nonatomic, retain) UILabel *pwdPromptLabel;

@end

@implementation LogInViewController

- (void)dealloc {
    [_pwdPromptLabel release];
    [_userPromptLabel release];
    [_userLTView release];
    [_pwdLTView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 页面布局
    [self createLogInView];
}


// 页面布局
- (void)createLogInView {
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"用户登录";
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    imgView.image = [UIImage imageNamed:@"123.jpg"];
    [self.view addSubview:imgView];
    
    // 返回
    UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(cancelButtonItemAction:)];
    self.navigationItem.leftBarButtonItem = cancelButtonItem;
    [cancelButtonItem release];
    
    // 用户名
    self.userLTView = [[LTView alloc] initWithFrame:CGRectMake(VIEW_CENTER_X - (9 * WIDTH / 10) / 2, VIEW_CENTER_Y / 6, 9 * WIDTH / 10, 50)];
    _userLTView.myLabel.text = @"用户名：";
    _userLTView.myTextField.placeholder = @"请输入用户名";
//    _userLTView.myTextField.clearsOnBeginEditing = YES;
    [self.view addSubview:_userLTView];
    [_userLTView release];
    _userLTView.myTextField.tag = 1000;
    _userLTView.myTextField.delegate = self;
    
    // 未输入用户名提示栏
    self.userPromptLabel = [[UILabel alloc] initWithFrame:CGRectMake(VIEW_CENTER_X - (9 * WIDTH / 10) / 2 + _userLTView.frame.size.width / 3, VIEW_CENTER_Y / 6 + 50, 6 * WIDTH / 10, 20)];
    _userPromptLabel.textColor = [UIColor redColor];
    [self.view addSubview:_userPromptLabel];
    
    
    // 密码
    self.pwdLTView = [[LTView alloc] initWithFrame:CGRectMake(VIEW_CENTER_X - (9 * WIDTH / 10) / 2, VIEW_CENTER_Y / 6 + 70, 9 * WIDTH / 10, 50)];
    _pwdLTView.myLabel.text = @"密码：";
    _pwdLTView.myTextField.placeholder = @"请输入密码";
    _pwdLTView.myTextField.secureTextEntry = YES;
//    _pwdLTView.myTextField.clearsOnBeginEditing = YES;
    [self.view addSubview:_pwdLTView];
    [_pwdLTView release];
    _pwdLTView.myTextField.tag = 1001;
    _pwdLTView.myTextField.delegate = self;
    
    // 未输入密码栏
    self.pwdPromptLabel = [[UILabel alloc] initWithFrame:CGRectMake(VIEW_CENTER_X - (9 * WIDTH / 10) / 2 + _userLTView.frame.size.width / 3, VIEW_CENTER_Y / 6 + 120, 6 * WIDTH / 10, 20)];
    _pwdPromptLabel.textColor = [UIColor redColor];
    [self.view addSubview:_pwdPromptLabel];
    
    // 登录按钮
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = CGRectMake(VIEW_CENTER_X - WIDTH / 2.5, VIEW_CENTER_Y / 1.5, WIDTH / 3, 50);
    [self.view addSubview:loginButton];
    [loginButton setTitle:@"登录" forState:(UIControlStateNormal)];
    [loginButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    loginButton.layer.cornerRadius = 5;
    loginButton.layer.borderWidth = 2;
    loginButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    loginButton.backgroundColor = [UIColor greenColor];
    [loginButton addTarget:self action:@selector(loginButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    // 注册按钮
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(VIEW_CENTER_X + (VIEW_CENTER_X - WIDTH / 2.3), VIEW_CENTER_Y / 1.5, WIDTH / 3, 50);
    [self.view addSubview:registerButton];
    [registerButton setTitle:@"注册" forState:(UIControlStateNormal)];
    [registerButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    registerButton.layer.cornerRadius = 5;
    registerButton.layer.borderWidth = 2;
    registerButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    registerButton.backgroundColor = [UIColor orangeColor];
    [registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

// 返回
- (void)cancelButtonItemAction:(UIBarButtonItem *)buttonItem {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

// 登录
- (void)loginButtonAction:(UIButton *)button {
    // 如果用户名为空
    if (!_userLTView.myTextField.text.length) {
        _userPromptLabel.text = @"* 请输入用户名";
        // 如果密码为空
    } else if (!_pwdLTView.myTextField.text.length) {
        _pwdPromptLabel.text = @"* 请输入密码";
        // 都不为空
    } else {
        [self dismissViewControllerAnimated:YES completion:^{
            [self.delegate changeRightButtonTitle:@"注销"];
        }];
    }    
}

// 注册
- (void)registerButtonAction:(UIButton *)button {
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    registerVC.accessBlock = ^(void) {
        [self.navigationController popToRootViewControllerAnimated:YES];
        UIAlertController *accessAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:accessAlert animated:YES completion:^{
            [self performSelector:@selector(time) withObject:self afterDelay:0.5];
        }];
        
    };
    [self.navigationController pushViewController:registerVC animated:YES];
    [registerVC release];
}

- (void)time {
    [self dismissViewControllerAnimated:YES completion:nil];
}



#pragma mark - TextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    UITextField *Textfiled2 = [self.view viewWithTag:textField.tag + 1];
    [textField resignFirstResponder];
    [Textfiled2 becomeFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    _userPromptLabel.text = @"";
    _pwdPromptLabel.text = @"";
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
